// pages/exam/exam.js
const App = getApp();
const {
  get,
  post
} = App.require('request/index')

import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: '', //倒计时
    totleList: [],
    index: 0,
    answer: {},
    disabled: false,
    navHeight:App.globalData.navHeight,
    subObj: {},
    time: null,
    timeout:"",
    clear:"",
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
  },
   
  countdown() {

    var minute = Math.floor(this.data.time / 60);
    var second = this.data.time % 60
    if (second < 10) {
      second = '0' + second;
      this.setData({
        countdown: minute + ':' + second
      })  
    } else {
      this.setData({
        countdown: minute + ':' + second
      })
    }
    this.setData({
      time: this.data.time - 1
    })
    if (this.data.countdown == "0:00" || this.data.countdown == "00:00") {
      this.setData({
        clear:clearTimeout(this.countdown)
      })
      
      this.submit()
    } else {
      this.setData({
        timeout:setTimeout(this.countdown, 1000)
      })
      
    }

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let opt = JSON.parse(options.data)

    // wx.setStorageSync('opt', JSON.stringify(opt));
    // let obj = JSON.parse(wx.getStorageSync('opt'));
    console.log(opt);

    let totleList = [...opt.radioList, ...opt.multiList, ...opt.judgeList]

    // let totleList = [...opt.radioList,...opt.multiList,...opt.judgeList]

    // if (opt) {
    //   this.setData({
    //     totleList: totleList
    //   })
    //   this.getpaperqudetail();
    // }

    if (opt) {
      this.setData({
        totleList: totleList,
        time: opt.totalTime * 60
      })
      this.countdown()
      this.getpaperqudetail();
    }

  },
  // 强制交卷

  change(e) {
  
    this.setData({
      subObj: {
        ...e.detail
      },
     
    })
  },
  // 提交试卷

  submt() {
   
    clearTimeout(this.data.timeout);
    this.submine();
   
    Dialog.confirm({
        title: '提示',
        message: '确定要交卷吗？',
      })
      .then(() => {
        this.submit()
      })
      .catch(() => {
        // on cancel
      });

  },
  submit() {
    let {
      answer,
      prolist
    } = this.data
    post('/app/exam/paper/handExam', {
      id: answer.paperId,
      memberId: prolist.userid
    }).then(res => {
     
      if (res.success) {
        wx.showLoading({
          title: '提交成功',
        })
        wx.hideLoading()
        wx.navigateTo({
          url: `/pages/examadmin/pages/examdetails/examdetails?id=${answer.paperId}`,
        })
      }else{
        wx.showLoading({
          title: res.msg,
        })
        wx.hideLoading()
      }
    })
  },

  // 提交题目
  submine() {
    let {
      subObj
    } = this.data
   
    post('/app/exam/paper/PaddingAnswer', {
      answers: [subObj.id],
      paperId: subObj.paperId,
      quId: subObj.quId
    }).then(res => {

      this.setData({
        subObj: {}
      })
    })
  },
  // 下一题
  lower() {
    this.submine()
    let {
      index,
      totleList
    } = this.data
    index++
    this.setData({
      index: index
    })
    if (index >= totleList.length - 1) {
      this.getpaperqudetail();
      this.setData({
        disabled: true,
      })
      return
    }

    this.getpaperqudetail();
  },
  // 上一题
  upper() {
    this.submine()
    let {
      index,
      totleList
    } = this.data
    index--
    this.setData({
      index: index--,
      disabled: false,
    })
    this.getpaperqudetail();
  },

  getpaperqudetail() {
    wx.showLoading({
      title: '加载中',
    })
    let {
      totleList,
      index
    } = this.data

    post('/app/exam/paper/quDetail', {
      paperId: totleList[index].paperId,
      quId: totleList[index].quId
     
    }).then(res => {
      console.log(res);
      this.setData({
        answer: {
          ...res.data
        }
      })
      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})